package com.dao;

import com.bean.教师情况;
import com.jdbc.DataSourceTool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class 教师情况Dao {
    //增
    public boolean insertTcr(教师情况 tcr) throws SQLException {
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "insert into 教师情况(教师编号,姓名,性别,职称,院系代号)" +
                "values (?,?,?,?,?)";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //给sql赋值
            ps.setString(1,tcr.get教师编号());
            ps.setString(2,tcr.get姓名());
            ps.setString(3, tcr.get性别());
            ps.setString(3, tcr.get职称());
            ps.setString(5, tcr.get院系代号());

            //执行sql
            int resul = ps.executeUpdate();
            //
            if(resul>0){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return false;
    }
    //删
    public boolean deleteTcr(String 教师编号) throws SQLException {
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "delete from 教师情况 where 教师编号 = ?";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //给sql赋值
            ps.setString(1,教师编号);

            //执行sql
            int resul = ps.executeUpdate();
            //
            if(resul>0){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return false;
    }

    //改
    public boolean updateTcr(教师情况 tcr) throws SQLException {
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "update 教师情况 set 姓名=?,性别=?,职称=?,院系代号=? where 教师编号=?; ";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //给sql赋值
            ps.setString(1,tcr.get姓名());
            ps.setString(2, tcr.get性别());
            ps.setString(3,tcr.get职称());
            ps.setString(4, tcr.get院系代号());
            ps.setString(1,tcr.get教师编号());
            //执行sql
            int resul = ps.executeUpdate();
            //
            if(resul>0){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return false;
    }
    //查 -- 返回结果集
    public List<教师情况> queryTcr() throws SQLException {
        //创建集合用于保存数据
        List<教师情况> list = new ArrayList<>();
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "select * from 教师情况;";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //执行sql
            ResultSet rs = ps.executeQuery();
            //遍历结果集
            while(rs.next()){
                //创建一个教师情况类型的数据用于获取数据库中相同类型的数据
                教师情况 tcr=new 教师情况();
                tcr.set教师编号(rs.getString(1));
                tcr.set姓名(rs.getString(2));
                tcr.set性别(rs.getString(3));
                tcr.set职称(rs.getString(4));
                tcr.set院系代号(rs.getString(5));

                //把查询结果放入集合中
                list.add(tcr);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return list;
    }
}
